package com.invest.utils.httpclient;

import retrofit.ErrorHandler;
import retrofit.RetrofitError;
import retrofit.client.Response;

import java.net.SocketTimeoutException;

/**
 * Created by evan on 2016/6/9.
 */
public class RetrofitErrorHandler implements ErrorHandler {
    @Override
    public Throwable handleError(RetrofitError cause) {

        //网络异常
        if(cause.isNetworkError()){
            if (cause.getCause() instanceof SocketTimeoutException) {

                return new RequestTimeoutException();
            }
            return cause.getCause();
        }

        Response r = cause.getResponse();
        //系统异常
        if (r != null) {
            int status = r.getStatus();

            if(status == StatusCode.EX_SYS){
                return new SysException();
            }

            if(status == StatusCode.NOT_AUTHORIZE){
                return new AuthorizeException();
            }

        }
        return cause;
    }

    public static class NetworkException extends RuntimeException{
        private static final long serialVersionUID = 857030269437838538L;
    }
    public static class RequestTimeoutException extends RuntimeException{
        private static final long serialVersionUID = -6108746755800959402L;
    }
    public static class SysException extends RuntimeException{
        private static final long serialVersionUID = 4210861481830041638L;
    }
    public static class AuthorizeException extends RuntimeException{

        private static final long serialVersionUID = -7871884329646087525L;
    }
}
